package com.linkoog.devtools.dubbo.persistent;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.project.Project;
import com.intellij.util.xmlb.XmlSerializerUtil;
import com.linkoog.devtools.dubbo.toolwindow.environment.DubboEnvironment;
import com.linkoog.devtools.dubbo.toolwindow.workbenche.left.DubboFavorite;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

@State(name = "DubboFavorite", storages = { @Storage("dubbo-favorite.settings.xml") })
public class DubboFavouritePersistent implements PersistentStateComponent<DubboFavouritePersistent> {

    private List<DubboFavorite> dubboFavorites;


    public DubboFavouritePersistent() {
    }

    public static DubboFavouritePersistent getInstance(@NotNull Project project){
        return project.getService(DubboFavouritePersistent.class);
    }

    @Override
    public DubboFavouritePersistent getState() {
        return this;
    }

    @Override
    public void loadState(@NotNull final DubboFavouritePersistent state) {
        XmlSerializerUtil.copyBean(state, this);
    }


    /*-------------------------------------------------------------------------------------------------------------------*/

    public List<DubboFavorite> getDubboFavorites() {
        if (dubboFavorites == null) {
            dubboFavorites = new ArrayList<>();
        }
        return dubboFavorites;
    }

    public void setDubboFavorites(List<DubboFavorite> dubboFavorites) {
        this.dubboFavorites = dubboFavorites;
    }
}
